package com.example.xinyun.slice;

import com.example.xinyun.MyApplication;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.TbFourm;
import com.example.xinyun.db.db_User;
import com.google.gson.Gson;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.utils.net.Uri;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FPinglunAbilitySlice extends AbilitySlice {
    public static final int REQUEST_CODE_CHOOSE = 1001;
    private List<Uri> mSelected;
    private String imagepath = "";
    private ImagePicker imagePicker;
    private ArrayList<ImageItem> images = new ArrayList<>();
    private Image mt3;
    private TextField mt1, mt2;
    private File mfile;
    private String imagename;


    private final EventHandler myEventHandler =
            new EventHandler(EventRunner.getMainEventRunner()) {
                @Override
                protected void processEvent(InnerEvent event) {
                    super.processEvent(event);

                    switch (event.eventId) {
                        case 0:
                            new ToastDialog(getAbility())
                                    .setText("发布成功")
                                    .show();
                            getAbility().terminateAbility();
                            break;
                        case 1:
                            new ToastDialog(getAbility())
                                    .setText("发布失败")
                                    .show();
                            break;
                        default:
                            break;
                    }
                }
            };


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_pl);

        mt1 = (TextField) findComponentById(ResourceTable.Id_t1);
        Text fb = (Text) findComponentById(ResourceTable.Id_fb);

        fb.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (mt1.getText().toString().isEmpty()) {
                    new ToastDialog(getAbility())
                            .setText("评论不能为空")
                            .show();
                    return;
                }
                TbFourm fourm = new TbFourm();
                fourm.setPinglun("");
                fourm.setLiulan(0);
                fourm.setId(0);
                fourm.setDianzan(0);
                fourm.setText(mt1.getText().toString().trim());
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
                fourm.setTime(simpleDateFormat.format(new Date()));
                fourm.setTzid(getAbility().getIntent().getIntParam("id", 0));
                fourm.setUser(MyApplication.username);

                ZZRHttp.postJson(UrlContant.URL + "addFourm", new Gson().toJson(fourm), new ZZRCallBack.CallBackString() {
                    @Override
                    public void onFailure(int i, String s) {

                            new ToastDialog(FPinglunAbilitySlice.this)
                                    .setText("评论失败，请检查网络连接")
                                    .show();

                    }

                    @Override
                    public void onResponse(String s) {
                        db_User db_user1 = new Gson().fromJson(s, db_User.class);
                        if (db_user1.getCode().equals("200")) {
                            new ToastDialog(FPinglunAbilitySlice.this)
                                    .setText("评论成功")
                                    .show();
                            getAbility().terminateAbility();
                        }else{
                            new ToastDialog(FPinglunAbilitySlice.this)
                                    .setText("评论失败")
                                    .show();
                        }
                    }
                });
            }
        });
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
